package org.lansir.af.view;

import java.util.ArrayList;
import java.util.List;

import org.lansir.af.R;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

public class WidgetQuickActionDialog extends PopupWindow {
	private ListView lvQuickAction;
	private WidgetQuickActionAdapter mAdapterQuickAction;

	private LayoutInflater mInflater;
	private List<WidgetQuickActionDialogItem> mListQuickActionItems = new ArrayList<WidgetQuickActionDialogItem>();

	public WidgetQuickActionDialog(Context context) {
		super(context);
		this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View mainView = mInflater.inflate(R.layout.widgets_quick_actions_dialog_vertical, null);
		lvQuickAction = (ListView) mainView.findViewById(R.id.lvQuickAction);
		
		mAdapterQuickAction = new WidgetQuickActionAdapter();
		lvQuickAction.setAdapter(mAdapterQuickAction);
		setContentView(mainView);
		setWidth(180);
		setHeight(LayoutParams.WRAP_CONTENT);
		
		setFocusable(true);
		setBackgroundDrawable(new BitmapDrawable());
//		setOutsideTouchable(true);

		lvQuickAction.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				mListQuickActionItems.get(position).getOnClickListener().onClick(view);
			}
			
		});
		
	}
	
	public void addMenuItems(WidgetQuickActionDialogItem menuPopupWindowItem){
		mListQuickActionItems.add(menuPopupWindowItem);
		mAdapterQuickAction.notifyDataSetChanged();

	}
	
	private class WidgetQuickActionAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return mListQuickActionItems.size();
		}

		@Override
		public Object getItem(int position) {
			return mListQuickActionItems.get(position);
		}

		@Override
		public long getItemId(int position) {
			return mListQuickActionItems.get(position).hashCode();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder;
			if(convertView == null){
				convertView = mInflater.inflate(R.layout.widgets_quick_actions_item_vertical, null);
				viewHolder = new ViewHolder();
				viewHolder.ivQuickActionVerticalItem = (ImageView) convertView.findViewById(R.id.ivQuickActionVerticalItem);
				viewHolder.tvQuickActionVertical = (TextView) convertView.findViewById(R.id.tvQuickActionVertical);
				convertView.setTag(viewHolder);
			}else{
				viewHolder = (ViewHolder) convertView.getTag();
			}
			
			viewHolder.ivQuickActionVerticalItem.setBackgroundResource(mListQuickActionItems.get(position).getImageRes());
			viewHolder.tvQuickActionVertical.setText(mListQuickActionItems.get(position).getTextRes());
			
			
			return convertView;
		}
		
		private class ViewHolder{
			public ImageView ivQuickActionVerticalItem;
			public TextView tvQuickActionVertical;
		}
		
	};
}
